var prefix = jsctx + "/elecPrinter";

var elecPrinterStation = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageElecPrinterStation",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            printerId: $("#printerId").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'printerName', title: '打印机名称', align: 'left', width: '100'},
                        {field: 'printerRtua', title: '打印机逻辑地址', align: 'left', width: '100'},
                        {field: 'stationName', title: '站点名称', align: 'left', width: '100'},
                        {
                            field: 'status', title: '状态', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '正常'
                                } else if (value == '1') {
                                    return '禁用'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: '',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                if (row.status == '0') {
                                    var a = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="禁用" onclick="elecPrinterStation.updateStatus(\''
                                        + row.printerStationId
                                        + '\')">禁用</a> ';
                                } else if (row.status == '1') {
                                    var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="启动" onclick="elecPrinterStation.updateStatus(\''
                                        + row.printerStationId
                                        + '\')">启动</a> ';
                                }
                                return a;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }

    function updateStatus(printerStationId) {
        layer.confirm('确定要更改状态？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/updateStatus",
                type: "post",
                data: {
                    'printerStationId': printerStationId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("更改成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    /*  模态框  */
    function showModel() {
        $('#exampleModal').modal('show')

        $("#addElecPrinter").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("elecStationId", $("#elecStationId").val());
            formData.append("printerId", $("#printerId").val());
            $.ajax({
                url: prefix + "/saveElecPrinterStation",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })

    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        showModel: showModel,
        updateStatus: updateStatus
    };
})();
